'use strict';
import crypto from 'crypto';
export default class extends think.service.base {

    init() {
        let buf = crypto.randomBytes(16);
        this.key = 'xxxxx';
        this.token = buf.toString('hex');
        // this.ak = 'e5c992c7e1ef4136890e2401a8740b6e';
        // this.sk = 'bd2f70e871d543ed81d43859afdaaccf';
        this.ak='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
        this.sk='bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
        this.d = new Date();
        // this.authStringPrefix='bce-auth-v1/'+ak+'/'+d.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate()+'-'+'T'+d.getHours()+':'+d.getMinutes()+':'+d.getSeconds()+'Z'+'/'+'3600';
        this.authStringPrefix = "bce-auth-v1/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/2015-04-27T08:23:49Z/1800//"
    }
    get(name) {
        return this[name]
    }
    encrypt(pwd) {
        try {
            var encrypted = "";
            var cip = crypto.createCipher('aes192', key);
            encrypted += cip.update(pwd.toString(), 'utf8', 'hex');
            encrypted += cip.final('hex');
            return encrypted
        } catch (err) {
            throw "encrypt加密失败"+err;
        }

    }
    decipher(pwd) {
        try {
            var decipher = crypto.createDecipher('aes192', key);
            var decrypted = decipher.update(pwd.toString(), 'hex', 'utf8') + decipher.final('utf8');
            return decrypted;
        } catch (err) {
            throw "decipher解密失败"+err;
        }
    }
    SigningKey(key,authStringPrefix) {
        try {
            var encrypted = "";
            var cip = crypto.createHmac('sha256', key).update(authStringPrefix).digest('hex');
            encrypted = cip;
            return encrypted
        } catch (err) {
            throw "SigningKey加密失败"+err;
        }

    }
    Signature(SigningKey,CanonicalRequest) {
        try {
            var encrypted = "";
            var cip = crypto.createHmac('sha256', SigningKey).update(CanonicalRequest).digest('hex');
            encrypted = cip;
            return encrypted
        } catch (err) {
            throw "Signature加密失败"+err;
        }
    }

    CanonicalQueryString(data){
        let queryString=[];
        Object.keys(data).forEach((key)=>{
            queryString.push(encodeURIComponent(key)+'='+encodeURIComponent(data[key]));
        })
        return queryString.join('&');
    }
    enCode(input, encodeSlash) {
        var result = "";
        for (let i = 0; i < input.length; i++) {
            let ch = input.charAt(i);
            if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '~' || ch == '.') {
                result += ch;
            } else if (ch == '/') {
                result += encodeSlash ? "%2F" : ch;
            } else {
                result += encodeURIComponent(ch);
            }
        }
        return result;
    }
    lexicographicalOrder(input) {
        let dictString = ''
        var sdic = Object.keys(input).sort();
        for (let ki of sdic) {
            dictString += ki.toLowerCase() + ':' + input[ki] + "\n";
        }
        return dictString;
    }
    paramsEncode(input){
        let qustring=[];
        Object.keys(input).forEach((key)=>{
            let value =input[key];
            qustring.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
        })
        qustring.sort();
        return qustring.join('&');
    }

}
